namespace DCHealthCheck.DomainModel {

    #region Usings
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Diagnostics;
    using System.Text;
    
    #endregion

    [Table("TagObject")]
    public class TagObject : ITagObject, IObjectWithState, IValidatableObject {

        #region Members
        #region Max length fields
        public static readonly int DNMaxLength = 900;
        public static readonly int DNSDomainMaxLength = 255;
        #endregion

        [Key]
        public virtual string DN { get; set; }
        public virtual string DNSDomain { get; set; }

        #region Related Entities
        public virtual ICollection<TagObjectValue> TagObjectValues {
            get {
                if (tagObjectValues == null) tagObjectValues = new List<TagObjectValue>();
                return tagObjectValues;
            }
            set { tagObjectValues = value; }
        }
        private ICollection<TagObjectValue> tagObjectValues;
        #endregion

        #region NotMapped properties
        [NotMapped]
        public DetachedState DetachedState { get; set; }

        [NotMapped]
        public Object EntityKey { get { return this.DN; } }

        [NotMapped]
        public Dictionary<string, object> StartingOriginalValues { get; set; }
        #endregion 
        #endregion

        #region Constructor
        public TagObject() {
        } 
        #endregion

        #region Methods
        [DebuggerStepThroughAttribute]
        public override string ToString() {
            var info = new StringBuilder();

            info.Append("TagObject: ");
            info.AppendFormat("DN: {0}; ", this.DN != null ? this.DN : "NULL");
            info.AppendFormat("DNSDomain: {0}", this.DNSDomain != null ? this.DNSDomain : "NULL");

            return info.ToString();
        }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
            if (string.IsNullOrWhiteSpace(this.DN)) {
                yield return new ValidationResult(
                    "DN is required.", new[] { "DN" });
            }
            else {
                if ((this.DN.IndexOf(@",DC=", StringComparison.CurrentCultureIgnoreCase) == -1) ||
                    (this.DN.IndexOf(@"CN=", StringComparison.CurrentCultureIgnoreCase) == -1)) {
                    yield return new ValidationResult("DN format invalid LDAP syntax.", new[] { "DN" });
                }

                if (this.DN.Length > DNMaxLength) {
                    yield return new ValidationResult(string.Format("DN length: {0} exceeds max length: {1}",
                        this.DN.Length, DNMaxLength), new[] { "DN" });
                }
            }

            if (this.DetachedState != DetachedState.Deleted) {
                if (string.IsNullOrWhiteSpace(this.DNSDomain)) {
                    yield return new ValidationResult(
                        "DNSDomain is required.", new[] { "DNSDomain" });
                }
                else {
                    if (this.DNSDomain.Length > DNSDomainMaxLength) {
                        yield return new ValidationResult(string.Format("DNSDomain length: {0} exceeds max length: {1}",
                            this.DNSDomain.Length, DNSDomainMaxLength), new[] { "DNSDomain" });
                    }
                }
            } // if (this.DetachedState != State.Deleted)
        }  
        #endregion
        
    }
}
